{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from deap import base,creator\n",
    "import random\n",
    "from deap import tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Anaconda3\\lib\\site-packages\\deap\\creator.py:141: RuntimeWarning: A class named 'FitnessMin' has already been created and it will be overwritten. Consider deleting previous creation of that class or rename it.\n",
      "  RuntimeWarning)\n",
      "d:\\Anaconda3\\lib\\site-packages\\deap\\creator.py:141: RuntimeWarning: A class named 'Individual' has already been created and it will be overwritten. Consider deleting previous creation of that class or rename it.\n",
      "  RuntimeWarning)\n"
     ]
    }
   ],
   "source": [
    "# Types\n",
    "creator.create(\"FitnessMin\", base.Fitness, weights=(-1.0,))\n",
    "# weights 1.0, 求最大值,-1.0 求最小值\n",
    "# (1.0,-1.0,)求第一个参数的最大值,求第二个参数的最小值\n",
    "creator.create(\"Individual\", list, fitness=creator.FitnessMin)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0.3950690605070669,\n",
       "  0.6077549987178139,\n",
       "  0.1192507866094521,\n",
       "  0.10793724938273697,\n",
       "  0.17026641280850496,\n",
       "  0.7973341684428628,\n",
       "  0.6450669273937372,\n",
       "  0.0709757872778809,\n",
       "  0.27105900056595966,\n",
       "  0.6997823164157441],\n",
       " [0.5310649889382006,\n",
       "  0.504009571665927,\n",
       "  0.7712200316974545,\n",
       "  0.21709369014849111,\n",
       "  0.14109129794470998,\n",
       "  0.3251786246526531,\n",
       "  0.7224224204700056,\n",
       "  0.4170691456746899,\n",
       "  0.8360834022824514,\n",
       "  0.8882536517372344],\n",
       " [0.9010355990469108,\n",
       "  0.8006856697847449,\n",
       "  0.25163797211018335,\n",
       "  0.2716245950406121,\n",
       "  0.772956736655453,\n",
       "  0.8578159885335043,\n",
       "  0.9903006855248606,\n",
       "  0.8760981062507791,\n",
       "  0.49501822956341834,\n",
       "  0.826722216442734],\n",
       " [0.6669643607941903,\n",
       "  0.1336668274777958,\n",
       "  0.16972749159495382,\n",
       "  0.6828340383721807,\n",
       "  0.04063638449656104,\n",
       "  0.05068102670335761,\n",
       "  0.1939951993439033,\n",
       "  0.6455132955525188,\n",
       "  0.7509956284808603,\n",
       "  0.7214423796449297],\n",
       " [0.9738244016923628,\n",
       "  0.4554004627607222,\n",
       "  0.24023721472240378,\n",
       "  0.9424327608528691,\n",
       "  0.5136979460580701,\n",
       "  0.9247704071106633,\n",
       "  0.2857211323363933,\n",
       "  0.15259545012928677,\n",
       "  0.4525235447379464,\n",
       "  0.19229491988406644]]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# Initialization\n",
    "IND_SIZE = 10  # 种群数\n",
    "\n",
    "toolbox = base.Toolbox()\n",
    "toolbox.register(\"attribute\", random.random)\n",
    "toolbox.attribute()\n",
    "\n",
    "\n",
    "# 调用randon.random为每一个基因编码编码创建 随机初始值 也就是范围[0,1]\n",
    "toolbox.register(\"individual\", tools.initRepeat, creator.Individual,\n",
    "                 toolbox.attribute, n=IND_SIZE)\n",
    "toolbox.individual()\n",
    "\n",
    "\n",
    "toolbox.register(\"population\", tools.initRepeat, list, toolbox.individual)\n",
    "toolbox.population(5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init pop: \n",
      "[[0.20704194117407082, 0.13968538652437223, 0.6537093763790898, 0.43469816551174656, 0.1665530967171327, 0.04079591578388564, 0.7453032326451163, 0.912393341174345, 0.10494876666782327, 0.1257010222754097], [0.13797190178533125, 0.26814053054921927, 0.6335433755863624, 0.1690511465083746, 0.7195402359155266, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, 0.20574558137429844], [0.0036707824839204983, 0.3274023581049863, 0.22016546823349936, 0.20597726961962115, 0.9809527943979199, 0.5862263279626893, 0.7816606852629361, 0.849659430129492, 0.07110145362550935, 0.673061556183817], [0.6156895683182905, 0.05083779706495872, 0.005173069634830774, 0.4347858199819834, 0.899064555953558, 0.09582464792693746, 0.4213723899220304, 0.7492973550232613, 0.7723889159371274, 0.31723066247854126], [0.28013868417478316, 0.759084323244714, 0.7931692976751931, 0.8380314376705916, 0.7745280562093038, 0.7112218954557465, 0.089903701135854, 0.7253546412007834, 0.06942139266467995, 0.5860504743131048], [0.9517132695909936, 0.970030257208567, 0.6087836959024396, 0.46503323156988285, 0.23966091366851494, 0.4221001803183697, 0.438502076864956, 0.7577230178261527, 0.3796466571099232, 0.969077048354082], [0.7978736422026455, 0.8766282257927146, 0.6633107176557846, 0.40711190070693604, 0.07909610673599576, 0.9646020585429989, 0.7577510835265188, 0.23583434906105583, 0.16228625560107401, 0.17839668588376056], [0.5101744045994585, 0.6780652443519092, 0.82748646527068, 0.3376228395216998, 0.34070147579845333, 0.11957974134388538, 0.6969712390936801, 0.2560627503155233, 0.018851494335222818, 0.25344688549783145], [0.7940418432041513, 0.17283261980720466, 0.42826722952814755, 0.8583814886277167, 0.4293493668368247, 0.14728709337499124, 0.9008159745912564, 0.8305910106300085, 0.04296388968653164, 0.9008688377909669], [0.25342159134896414, 0.3833319711972891, 0.07351007381062258, 0.747608009718374, 0.9812116954033587, 0.1607626436103914, 0.49165412629530847, 0.6241902977043521, 0.827929105827085, 0.48466953961019865]]\n",
      "-- Generation 0 --\n",
      "[0.13797190178533125, 0.26814053054921927, 0.6335433755863624, 0.40711190070693604, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, 0.20574558137429844] (2.842030194095365,)\n",
      "-- Generation 1 --\n",
      "[0.13797190178533125, 0.26814053054921927, 0.6335433755863624, 0.40711190070693604, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, 0.20574558137429844] (2.842030194095365,)\n",
      "-- Generation 2 --\n",
      "[0.13797190178533125, 0.26814053054921927, 0.6335433755863624, 0.40711190070693604, 0.07909610673599576, 0.04079591578388564, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, 0.20574558137429844] (2.55325590692824,)\n",
      "-- Generation 3 --\n",
      "[0.13797190178533125, 0.26814053054921927, 0.6335433755863624, 0.40711190070693604, 0.07909610673599576, 0.04079591578388564, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, 0.20574558137429844] (2.55325590692824,)\n",
      "-- Generation 4 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, -0.9132373531802501] (0.23527080813027557,)\n",
      "-- Generation 5 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, -0.9132373531802501] (0.23527080813027557,)\n",
      "-- Generation 6 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, -0.9132373531802501] (0.23527080813027557,)\n",
      "-- Generation 7 --\n",
      "[-1.35115719462358, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 1.4200874222301474, 0.07844246286952783, 0.16858580314043348, -0.9132373531802501] (-0.23329207514332706,)\n",
      "-- Generation 8 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, -0.9132373531802501] (0.23527080813027557,)\n",
      "-- Generation 9 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, -0.9132373531802501] (0.23527080813027557,)\n",
      "-- Generation 10 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, -0.9132373531802501] (0.23527080813027557,)\n",
      "-- Generation 11 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, -0.9132373531802501] (0.23527080813027557,)\n",
      "-- Generation 12 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, -0.9132373531802501] (0.23527080813027557,)\n",
      "-- Generation 13 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, -0.9132373531802501] (0.23527080813027557,)\n",
      "-- Generation 14 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, -0.9132373531802501] (0.23527080813027557,)\n",
      "-- Generation 15 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, -0.9132373531802501] (0.23527080813027557,)\n",
      "-- Generation 16 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, -0.9132373531802501] (0.23527080813027557,)\n",
      "-- Generation 17 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, 0.16858580314043348, -0.9132373531802501] (0.23527080813027557,)\n",
      "-- Generation 18 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -1.325641355272082, 0.7718772186869927, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, -0.35915220599428344, -0.9132373531802501] (0.021035987076667206,)\n",
      "-- Generation 19 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, -0.35915220599428344, -0.9132373531802501] (-0.29246720100444135,)\n",
      "-- Generation 20 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, -0.35915220599428344, -0.9132373531802501] (-0.29246720100444135,)\n",
      "-- Generation 21 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, -0.03340536535467373, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, -0.35915220599428344, -0.9132373531802501] (-0.40496867309511086,)\n",
      "-- Generation 22 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, -0.03340536535467373, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, -0.35915220599428344, -0.9132373531802501] (-0.40496867309511086,)\n",
      "-- Generation 23 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, -0.03340536535467373, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, -0.35915220599428344, -0.9132373531802501] (-0.40496867309511086,)\n",
      "-- Generation 24 --\n",
      "[0.0036707824839204983, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, -0.03340536535467373, 0.32957020295101047, 0.5338223283962494, 0.07844246286952783, -0.35915220599428344, -0.9132373531802501] (-0.40496867309511086,)\n",
      "-- Generation 25 --\n",
      "[-0.9779897721494565, 0.26814053054921927, 0.6335433755863624, -0.9463634314021935, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-2.3587408251506545,)\n",
      "-- Generation 26 --\n",
      "[-0.9779897721494565, 0.26814053054921927, 0.6335433755863624, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-3.460169196876579,)\n",
      "-- Generation 27 --\n",
      "[-0.9779897721494565, 0.26814053054921927, 0.6335433755863624, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-3.460169196876579,)\n",
      "-- Generation 28 --\n",
      "[-0.9779897721494565, 0.26814053054921927, 0.6335433755863624, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-3.460169196876579,)\n",
      "-- Generation 29 --\n",
      "[-0.368988803630491, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-5.066342986735728,)\n",
      "-- Generation 30 --\n",
      "[-0.368988803630491, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-5.066342986735728,)\n",
      "-- Generation 31 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-6.509044017882949,)\n",
      "-- Generation 32 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.5338223283962494, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-6.509044017882949,)\n",
      "-- Generation 33 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, -0.0639621744556661, 0.32957020295101047, 0.5338223283962494, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-6.652102299074611,)\n",
      "-- Generation 34 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-6.859978530290731,)\n",
      "-- Generation 35 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-6.859978530290731,)\n",
      "-- Generation 36 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-6.859978530290731,)\n",
      "-- Generation 37 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-6.859978530290731,)\n",
      "-- Generation 38 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-6.859978530290731,)\n",
      "-- Generation 39 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-6.859978530290731,)\n",
      "-- Generation 40 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-6.859978530290731,)\n",
      "-- Generation 41 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-6.859978530290731,)\n",
      "-- Generation 42 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-6.859978530290731,)\n",
      "-- Generation 43 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -0.9132373531802501] (-6.859978530290731,)\n",
      "-- Generation 44 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -1.4503928014230012] (-7.397133978533482,)\n",
      "-- Generation 45 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -1.4503928014230012] (-7.397133978533482,)\n",
      "-- Generation 46 --\n",
      "[-1.8116898347777108, 0.26814053054921927, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -1.4503928014230012] (-7.397133978533482,)\n",
      "-- Generation 47 --\n",
      "[-1.8116898347777108, 0.023270070082534816, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -1.5670964164492593] (-7.758708054026425,)\n",
      "-- Generation 48 --\n",
      "[-1.8116898347777108, 0.023270070082534816, -1.5816313827917519, -2.0477918031281184, 0.07909610673599576, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -1.5670964164492593] (-7.758708054026425,)\n",
      "-- Generation 49 --\n",
      "[-1.8116898347777108, 0.023270070082534816, -1.6916538981349425, -2.0477918031281184, -0.16642382358977886, 0.32957020295101047, 0.18288781598846698, -1.0061706066433085, -0.35915220599428344, -1.4503928014230012] (-7.997546884669132,)\n",
      "-- End of (successful) evolution --\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Operators\n",
    "# difine evaluate function\n",
    "# Note that a comma is a must\n",
    "def evaluate(individual):\n",
    "    return sum(individual),\n",
    "\n",
    "\n",
    "# use tools in deap to creat our application\n",
    "toolbox.register(\"mate\", tools.cxTwoPoint) # mate:交叉\n",
    "toolbox.register(\"mutate\", tools.mutGaussian, mu=0, sigma=1, indpb=0.1) # mutate : 变异\n",
    "toolbox.register(\"select\", tools.selTournament, tournsize=3) # select : 选择保留的最佳个体\n",
    "toolbox.register(\"evaluate\", evaluate)  # commit our evaluate\n",
    "\n",
    "\n",
    "\n",
    "# create an initial population of 300 individuals (where\n",
    "# each individual is a list of integers)\n",
    "pop = toolbox.population(n=10)\n",
    "CXPB, MUTPB, NGEN = 0.8, 0.1, 50\n",
    "\n",
    "'''\n",
    "# CXPB  is the probability with which two individuals\n",
    "#       are crossed\n",
    "#\n",
    "# MUTPB is the probability for mutating an individual\n",
    "#\n",
    "# NGEN  is the number of generations for which the\n",
    "#       evolution runs\n",
    "'''\n",
    "\n",
    "# Evaluate the entire population\n",
    "fitnesses = map(toolbox.evaluate, pop)\n",
    "for ind, fit in zip(pop, fitnesses):\n",
    "    ind.fitness.values = fit\n",
    "\n",
    "print('init pop: ')\n",
    "print(pop)\n",
    "\n",
    "for g in range(NGEN):\n",
    "    print(\"-- Generation %i --\" % g)\n",
    "    # Select the next generation individuals\n",
    "    offspring = toolbox.select(pop, len(pop))\n",
    "    # Clone the selected individuals\n",
    "    offspring = list(map(toolbox.clone, offspring))\n",
    "    # Change map to list,The documentation on the official website is wrong\n",
    "#     print(offspring.shape)\n",
    "    \n",
    "    # Apply crossover and mutation on the offspring\n",
    "    # 从offspring选出\n",
    "    for child1, child2 in zip(offspring[::2], offspring[1::2]):\n",
    "        if random.random() < CXPB:\n",
    "            toolbox.mate(child1, child2)\n",
    "            del child1.fitness.values\n",
    "            del child2.fitness.values\n",
    "\n",
    "    for mutant in offspring:\n",
    "        if random.random() < MUTPB:\n",
    "            toolbox.mutate(mutant)\n",
    "            del mutant.fitness.values\n",
    "\n",
    "    # Evaluate the individuals with an invalid fitness\n",
    "    invalid_ind = [ind for ind in offspring if not ind.fitness.valid]\n",
    "    fitnesses = map(toolbox.evaluate, invalid_ind)\n",
    "    for ind, fit in zip(invalid_ind, fitnesses):\n",
    "        ind.fitness.values = fit\n",
    "\n",
    "    # The population is entirely replaced by the offspring\n",
    "    pop[:] = offspring\n",
    "#     print(pop)\n",
    "    best_ind = tools.selBest(pop, 1)[0]\n",
    "    print(best_ind, best_ind.fitness.values)\n",
    "\n",
    "print(\"-- End of (successful) evolution --\")\n",
    "\n",
    "# best_ind = tools.selBest(pop, 1)[0]\n",
    "\n",
    "# print(best_ind, best_ind.fitness.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
